package com.pkk.cloud.support.oauth2.client.authurl.inter.impl;

import com.pkk.cloud.support.admin.oauth2.api.common.req.ResourceUrlAuth;
import com.pkk.cloud.support.oauth2.client.authurl.api.AuthToAccessApi;
import com.pkk.cloud.support.oauth2.client.authurl.inter.UrlAuthProvider;
import com.pkk.components.rpc.request.CommonRequest;
import com.pkk.components.rpc.response.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @description:接口是否有权限的默认实现
 * @author: peikunkun
 * @create: 2020-03-21 15:21
 **/
@Slf4j
public class OAuth2UrlAuthProvider implements UrlAuthProvider {

  @Autowired
  private AuthToAccessApi authToAccessApi;

  /**
   * 是否具备访问权限
   *
   * @param userName
   * @param reqUrl
   * @Return
   */
  @Override
  public Boolean accessAuth(String userName, String reqUrl) {
    log.debug("access auth userName:{}, reqUrl:{}", userName, reqUrl);
    ResourceUrlAuth requestUrl = new ResourceUrlAuth(reqUrl, userName);
    CommonRequest<ResourceUrlAuth> commonRequest = new CommonRequest();
    commonRequest.setBody(requestUrl);
    CommonResponse<Boolean> commonResponse = this.authToAccessApi.userAuthentication(commonRequest);
    log.debug("access auth response:{}", commonResponse);
    return null != commonResponse.getBody() ? (Boolean) commonResponse.getBody() : false;
  }

}
